function [filteredData, lostPercent] = FilterData(analytes,reducedData, anomalySVM, commonSVM )

allIDX=[];
allGoods=0;
allBads =0;

for K=1:length(analytes)
    
    idx = find(reducedData(:,1)==analytes(K));
    idx = idx(randperm(length(idx)));
    goods=0;
    
    for I=1:500:size(idx,1)
        outs =1+ zeros([500 1]);
        top = min([ size(idx,1) I+500]);
        i2=idx(I:top);
        temp= reducedData(i2,4:end);
        if isempty(anomalySVM )==false
            predictedGroupsA = svmoneclassval(temp,anomalySVM.xsup,anomalySVM.alpha,anomalySVM.rho,anomalySVM.kernel,anomalySVM.kerneloption);
            outs(predictedGroupsA<anomalySVM.threshold)=0;
        end
        
        if isempty(commonSVM )==false
            predictedGroupsB = svmoneclassval(temp,commonSVM.xsup,commonSVM.alpha,commonSVM.rho,commonSVM.kernel,commonSVM.kerneloption);
            outs(predictedGroupsB>=commonSVM.threshold)=0;
        end
        
        i2=i2(outs==1);
        goods = goods + length(i2);
        allGoods=allGoods + length(i2);
        allBads = allBads + abs(length(i2) -500);
        allIDX=[allIDX i2']; %#ok<AGROW>
        
        if goods >1000
            break;
        end
    end
end
filteredData=reducedData(allIDX,:);
lostPercent = allBads /(allGoods + allBads)*100;
end